package StudyCode.summary.BufferAndChannel;

import java.io.FileInputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FileConcatenationUsingChannelTransfer {
	public static void main(String[] argv) throws Exception {
		String[] files = new String[]{"E:/Copy/1.txt", "E:/Copy/2.txt"};
		System.out.println("开始关联文件:");
		catFiles(Channels.newChannel(System.out), files);
		System.out.println("关联文件结束:");
	}

	private static void catFiles(WritableByteChannel target, String[] files) throws Exception {
		for (String file : files) {
			FileInputStream fis = new FileInputStream(file);
			FileChannel channel = fis.getChannel();
			channel.transferTo(0, channel.size(), target);
			channel.close();
			fis.close();
		}
	}
}
